﻿//*
//*考试插件的一些方法
//*
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
using Library;

namespace OA.Exam
{
    public class Exam
    {
        ArrayList list = new ArrayList();

        /// <summary>
        /// 获取所有题型
        /// </summary>
        /// <returns></returns>
        public static DataTable GetQuestionTypes()
        {
            try
            {
                string sql = "select TypeId,TypeName from Exam_QuestionTypes";
                DataTable dt = DataAccess.getDataTable(sql);
                return dt;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 根据题目类型编号得到类型名称
        /// </summary>
        /// <param name="typeId">类型编号</param>
        /// <returns></returns>
        public string GetTypeName(string typeId)
        {
            string typeName = string.Empty;
            string sql = "select TypeName from Exam_QuestionTypes where TypeId = '" + typeId + "'";
            object obj = DataAccess.getObject(sql);
            if (obj != null)
            {
                typeName = obj.ToString();
            }
            return typeName;
        }

        /// <summary>
        /// 获取所有题目分组
        /// </summary>
        /// <returns></returns>
        public void GetQuestionGroups(DropDownList myList)
        {
            string sql = "select GroupId,GroupName,NodePath from Groups where GroupType = 'question' order by DispOrder";
            DataTable dt = DataAccess.getDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string nodePath = dt.Rows[i]["nodepath"].ToString();
                if (nodePath.Length > 1)
                {
                    string[] arr = nodePath.Split('.');
                    int num = arr.Length - 2;
                    string str = "";
                    for (int j = 1; j <= num; j++)
                    {
                        str += "--";
                    }
                    dt.Rows[i]["GroupName"] = str + dt.Rows[i]["GroupName"].ToString();
                }


            }
            myList.DataSource = dt;
            myList.DataValueField = "GroupId";
            myList.DataTextField = "GroupName";
            myList.DataBind();
        }

        /// <summary>
        /// 绑定分组树形结构
        /// </summary>
        /// <param name="myTree"></param>
        public void GetGroupTree(TreeView myTree)
        {
            myTree.Nodes.Clear();
            TreeNode rootNode = new TreeNode("所有分组", "all");//定义一个根节点
            //rootNode.SelectAction = TreeNodeSelectAction.None;
            rootNode.ShowCheckBox = false;
            myTree.Nodes.Add(rootNode);//附加在View上

            string sql = "select GroupId,GroupName from Groups where GroupType='question' and NodePath='.'";
            DataTable dt = DataAccess.getDataTable(sql);

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string groupId = dt.Rows[i]["GroupId"].ToString();
                    string groupName = dt.Rows[i]["GroupName"].ToString();
                    TreeNode firstNodes = new TreeNode(groupName, groupId);

                    if (HasChildNode(groupId))
                    {
                        TreeNode childNode = GetChildTree(firstNodes, groupId);
                        rootNode.ChildNodes.Add(childNode);
                    }
                    else
                    {
                        rootNode.ChildNodes.Add(firstNodes);
                    }
                    rootNode.ExpandAll();
                }
            }
        }

        /// <summary>
        /// 绑定已选分组
        /// </summary>
        /// <param name="myTree"></param>
        /// <param name="qId"></param>
        public void BindHasCheckGroup(TreeView myTree, string qId)
        {
            
            string sql = "select GroupId from Exam_QuestionsGroups where QuestionId='" + qId + "'";
            DataTable dt = DataAccess.getDataTable(sql);
            string groupId = string.Empty;

            TreeNodeCollection nodeCol = new TreeNodeCollection();
            if(dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    groupId = dt.Rows[i]["GroupId"].ToString();
                    MarkHadCheckNodes(myTree.Nodes[0], groupId);
                }
            }
        }

        /// <summary>
        /// 在树上标识已选择的分组
        /// </summary>
        /// <param name="node">结点</param>
        /// <param name="groupId">分组编号</param>
        public void MarkHadCheckNodes(TreeNode node, string groupId)
        {
            if (node == null)
            {
                return;
            }
            if (node.ChildNodes.Count > 0)
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    if (node.ChildNodes[i].Value == groupId)
                    {
                        node.ChildNodes[i].Checked = true;
                    }
                    MarkHadCheckNodes(node.ChildNodes[i], groupId);
                }
            }
        }

        /// <summary>
        /// 是否有子节点
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public static bool HasChildNode(string groupId)
        {
            bool flag = false;
            string sqlSecond = "select GroupId,GroupName from Groups where GroupType='question' and NodePath like '.'+'%" + groupId + "'+'.'";
            object obj = DataAccess.getObject(sqlSecond);
            if (obj != null)
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 得到一个节点的子树
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        private TreeNode GetChildTree(TreeNode fatherNode, string groupId)
        {

            string sql = "select GroupId,GroupName from Groups where GroupType='question' and NodePath like '.'+'%" + groupId + "'+'.'";
            DataTable dt = DataAccess.getDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string id = dt.Rows[i]["GroupId"].ToString();
                    string name = dt.Rows[i]["GroupName"].ToString();

                    TreeNode Node = new TreeNode(name, id);
                    //判断是否有子节点
                    if (HasChildNode((groupId + "." + id)))
                    {
                        Node.ChildNodes.Add(GetChildTree(Node, (groupId + "." + id)));
                    }
                    fatherNode.ChildNodes.Add(Node);
                }
            }
            return fatherNode;
        }
        
        /// <summary>
        /// 新增一道题目，只维护主体部分，不处理答案及解析
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="questionType"></param>
        /// <param name="subject"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public bool InsertQuestion(string questionId, string questionType, string subject, int score)
        {
            bool flag = false;
            string sql = "insert into Exam_Questions(QuestionId,Subject,ItemType,Score) values('" + questionId + "','" + subject + "','" + questionType + "'," + score + ")";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 修改题目，只维护主体部分，不处理答案及解析
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="questionType"></param>
        /// <param name="subject"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public bool UpdateQuestionItems(string questionId, string questionType, string subject, int score)
        {
            bool flag = false;
            string sql = "update Exam_Questions set Subject='" + subject + "',ItemType='" + questionType + "',Score=" + score + " where QuestionId='" + questionId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 根据题目编号获取该题的一条记录
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public DataTable GetRecordOfQuestion(string questionId)
        {
            DataTable dt = null;
            string sql = "select subject,itemtype,score,answer,answerex from Exam_Questions where questionId = '" + questionId + "'";
            dt = DataAccess.getDataTable(sql);
            return dt;
        }

        /// <summary>
        /// 根据题目编号获取题目选项列表
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public DataTable GetItemListOfQuestion(string questionId)
        {
            DataTable dt = null;
            string sql = "select * from Exam_QuestionItems where QuestionId='" + questionId + "'";
            try
            {
                dt = DataAccess.getDataTable(sql);
                return dt;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 插入题目的答案及解析，主要针对判断题和问答题
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public bool InsertAnswerNoItem(string questionId,string answer,string answerEx)
        {
            bool flag = false;
            string sql = "update Exam_Questions set answer='" + answer + "',answerEx='" + answerEx + "' where questionId='" + questionId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 设置选择题答案
        /// </summary>
        /// <param name="questionId">题目编号</param>
        /// <param name="answer">答案</param>
        /// <param name="answerEx">答案解析</param>
        /// <returns></returns>
        public bool SetCheckQuestionAnswer(string questionId, string answer, string answerEx)
        {
            bool flag = false;
            string sql = "update Exam_Questions set answer='" + answer + "',answerEx='"+answerEx+"' where questionid='" + questionId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 插入题目的选项，针对选择题和填空题
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="itemNum"></param>
        /// <param name="itemContent"></param>
        /// <returns></returns>
        public bool InsertQuestionItems(string questionId, string itemNum, string itemContent)
        {
            bool flag = false;
            string sql = "insert into Exam_QuestionItems values('" + questionId + "','" + itemNum + "','" + itemContent + "')";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 根据题目编号返回生成的选项号
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public string GetItemNumsOfQuestion(string questionId)
        {
            string itemNum = string.Empty;//返回的可用选项号
            string hadNums = string.Empty;//已有的选项号字符串，逗号隔开每一项
            string sql = "select ItemNum from Exam_QuestionItems where questionId='" + questionId + "'";
            DataTable dt = DataAccess.getDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                //拼装hadNums
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    hadNums += dt.Rows[j]["ItemNum"].ToString() + ",";
                }
                hadNums = hadNums.TrimEnd(',');
                //比较
                for (int i = 65; i < 89; i++)
                {
                    if (hadNums.IndexOf(Convert.ToChar(i)) != -1)
                    {
                        continue;
                    }
                    else
                    {
                        itemNum = Convert.ToChar(i).ToString();
                        break;
                    }
                }
            }
            else
            {
                itemNum = Convert.ToChar(65).ToString();//返回'A'
            }
            return itemNum;
        }

        /// <summary>
        /// 根据题目编号和选项号删除一个选项
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="itemNum"></param>
        /// <returns></returns>
        public bool DeleteItemOfQuestion(string questionId, string itemNum)
        {
            bool flag = false;
            string sql = "delete from Exam_QuestionItems where questionId='" + questionId + "' and itemnum='" + itemNum + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 获取选项内容
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="itemNum"></param>
        /// <returns></returns>
        public string GetContentOfItem(string questionId, string itemNum)
        {
            string itemContent = string.Empty;
            string sql = "select ItemContent from Exam_QuestionItems where questionId='" + questionId + "' and itemnum='" + itemNum + "'";
            object obj = DataAccess.getObject(sql);
            if (obj != null)
            {
                itemContent = obj.ToString();
            }
            return itemContent;
        }

        /// <summary>
        /// 修改题目选项内容
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="itemNum"></param>
        /// <param name="itemContent"></param>
        /// <returns></returns>
        public bool UpdateQuestionItems(string questionId, string itemNum, string itemContent)
        {
            bool flag = false;
            string sql = "update Exam_QuestionItems set itemcontent= '" + itemContent + "' where questionid='" + questionId + "' and itemnum='" + itemNum + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 判断某一题目是否包含于试卷中
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public bool IsInPaper(string questionId)
        {
            bool flag = false;
            string sql = "select PaperId from Exam_PapersQuestions where questionid='" + questionId + "'";
            object obj = DataAccess.getObject(sql);
            if (obj != null)
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 删除某一题目，同时删除题目分组和题目选项
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public bool DeleteQuestion(string questionId)
        {
            bool flag = false;
            string sqlMain = "delete from Exam_Questions where questionid='" + questionId + "'";
            string sqlGroup = "delete from Exam_QuestionsGroups where questionid='" + questionId + "'";
            string sqlItem = "delete from Exam_QuestionItems where questionid='" + questionId + "'";
            list.Add(sqlMain);//主表
            list.Add(sqlGroup);//分组表
            list.Add(sqlItem);//选项表
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 新增一套试卷，同时试卷大题表中新增所有题目类型
        /// </summary>
        /// <param name="paperId"></param>
        /// <param name="paperName"></param>
        /// <param name="score"></param>
        /// <param name="status">1未使用，2考试中，3考试结束</param>
        /// <param name="description"></param>
        /// <param name="examTime"></param>
        /// <returns></returns>
        public bool InsertPaper(string paperId, string paperName, int score, string status, string description, int examTime)
        {
            bool flag = false;
            string typeId = string.Empty;
            string sqlType = string.Empty;
            string sql = "insert into Exam_Papers values('" + paperId + "','" + paperName + "'," + score + ",'" + status + "','" + description + "'," + examTime + ")";
            list.Add(sql);
            foreach (DataRow eachRow in GetQuestionTypes().Rows)//遍历题目类型
            {
                typeId = eachRow["TypeId"].ToString();
                sqlType = "insert into Exam_PapersQuestionTypes values('" + paperId + "','" + typeId + "',0,0)";
                list.Add(sqlType);
            }
            
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 修改试卷，仅在未使用状态下可修改
        /// </summary>
        /// <param name="paperId"></param>
        /// <param name="paperName"></param>
        /// <param name="score"></param>
        /// <param name="status"></param>
        /// <param name="description"></param>
        /// <param name="examTime"></param>
        /// <returns></returns>
        public bool UpdatePaper(string paperId, string paperName, int score, string status, string description, int examTime)
        {
            bool flag = false;
            string sql = "update Exam_Papers set PaperName='" + paperName + "',score=" + score + ",status='" + status + "',description='" + description + "',examtime=" + examTime + " where PaperId='" + paperId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 根据试卷编号获取试卷的一条记录
        /// </summary>
        /// <param name="paperId"></param>
        /// <returns></returns>
        public DataTable GetRecordOfPaper(string paperId)
        {
            DataTable dt = null;
            string sql = "select papername,score,status,description,examtime from Exam_Papers where paperId = '" + paperId + "'";
            dt = DataAccess.getDataTable(sql);
            return dt;
        }

        /// <summary>
        /// 删除一套试卷，同时删除试卷中题目记录和大题类型记录
        /// </summary>
        /// <param name="paperId"></param>
        /// <returns></returns>
        public bool DeletePaper(string paperId)
        {
            bool flag = false;
            //主表
            string sqlMain = "delete from Exam_Papers where PaperId='" + paperId + "'";
            //从表
            string sqlRecord = "delete from Exam_PapersQuestions where PaperId='" + paperId + "'";
            string sqlType = "delete from Exam_PapersQuestionTypes where PaperId='" + paperId + "'";
            list.Add(sqlMain);
            list.Add(sqlRecord);
            list.Add(sqlType);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 删除试卷中某一类大题
        /// </summary>
        /// <param name="paperId">试卷编号</param>
        /// <param name="typeId">题目类型编号</param>
        /// <returns></returns>
        public bool DeletePaperBigType(string paperId, string typeId)
        {
            bool flag = false;
            string sqlMain = "delete from Exam_PapersQuestionTypes where PaperId='" + paperId + "' and ItemType='" + typeId + "'";
            
            list.Add(sqlMain);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 为试卷中加入一类大题
        /// </summary>
        /// <param name="paperId">试卷编号</param>
        /// <param name="typeId">题目类型编号</param>
        /// <returns></returns>
        public bool InsertPaperBigType(string paperId, string typeId)
        {
            bool flag = false;
            string sql = "insert into Exam_PapersQuestionTypes values('" + paperId + "','" + typeId + "',0,0)";

            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 为题目选择一个分组
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public bool InsertGroup(string questionId, string groupId)
        {
            list.Clear();
            bool flag = false;
            string sql = "insert into Exam_QuestionsGroups values('" + questionId + "','" + groupId + "')";

            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 为题目删除一个分组
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public bool DeleteGroup(string questionId, string groupId)
        {
            bool flag = false;
            string sql = "delete from Exam_QuestionsGroups where QuestionId='" + questionId + "' and GroupId='" + groupId + "'";

            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 删除题目的所有已选分组
        /// </summary>
        /// <param name="questionId"></param>
        public bool DeleteAllGroups(string questionId)
        {
            bool flag = false;
            string sql = "delete from Exam_QuestionsGroups where questionid='" + questionId + "'";

            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 修改试卷大题设置
        /// </summary>
        /// <param name="paperId"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public bool UpdatePaperBigType(string paperId, string typeId, int score, int dispOrder)
        {
            bool flag = false;
            string sql = "update Exam_PapersQuestionTypes set score=" + score + ",disporder=" + dispOrder + " where paperid='" + paperId + "' and itemtype='" + typeId + "'";

            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 为试卷插入一道题目
        /// </summary>
        /// <param name="paperId"></param>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public bool InsertPaperQuestion(string paperId, string questionId)
        {
            bool flag = false;
            string sql = "insert into Exam_PapersQuestions(PaperId,QuestionId) values('" + paperId + "','" + questionId + "')";
            list.Clear();
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 为试卷删除一道题目
        /// </summary>
        /// <param name="paperId"></param>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public bool DeletePaperQuestion(string paperId, string questionId)
        {
            bool flag = false;
            string sql = "delete from Exam_PapersQuestions where PaperId='" + paperId + "' and QuestionId='" + questionId + "'";
            list.Clear();
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        #region 试卷生成
        /// <summary>
        /// 绑定试卷大题下的小题
        /// </summary>
        /// <param name="paperId"></param>
        /// <param name="itemType"></param>
        /// <returns></returns>
        public DataTable GetSmallQuestions(string paperId, string itemType)
        {
            DataTable dt = null;
            string sql = "select a.QuestionId,Subject,ItemType,a.Score as EachScore,answer,answerex from Exam_PapersQuestions as a left join Exam_Questions as b on a.QuestionId=b.QuestionId where PaperId='" + paperId + "' and ItemType='" + itemType + "'";
            dt = DataAccess.getDataTable(sql);
            return dt;
        }
        /// <summary>
        /// 设置主观题每题分值
        /// </summary>
        /// <param name="paperId">试卷编号</param>
        /// <param name="questionId">题目编号</param>
        /// <param name="score">分值</param>
        public bool SetScore(string paperId, string questionId, int score)
        {
            bool flag = false;
            string sql = "update Exam_PapersQuestions set score=" + score + " where paperid='" + paperId + "' and questionid='" + questionId + "'";

            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 修改试卷总分
        /// </summary>
        /// <param name="paperId"></param>
        /// <param name="totalScore"></param>
        /// <returns></returns>
        public bool UpdateTotalScore(string paperId,int totalScore)
        {
            bool flag = false;
            string sql = "update Exam_Papers set score=" + totalScore + " where paperid='" + paperId + "'";

            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 得到试卷中一道主观题的分值
        /// </summary>
        /// <param name="paperId">试卷编号</param>
        /// <param name="questionId">题目编号</param>
        /// <returns></returns>
        public int GetSubjectiveScoreOfQuestion(string paperId, string questionId)
        {
            int score = 0;
            string sql = "select score from Exam_PapersQuestions where paperid='" + paperId + "' and questionId='" + questionId + "'";
            object obj = DataAccess.getObject(sql);
            if (obj != null)
            {
                score = int.Parse(obj.ToString());
            }
            return score;
        }
        /// <summary>
        /// 修改考试状态
        /// </summary>
        /// <param name="paperId">试卷编号</param>
        /// <param name="status">可用状态1考试中，2考试结束</param>
        /// <returns></returns>
        public bool UpdateStatusOfPaper(string paperId, string status)
        {
            bool flag = false;
            string sql = "update Exam_Papers set status='" + status + "' where paperid='" + paperId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 考卷
        /// <summary>
        /// 获得某考试参加该试卷的状态
        /// </summary>
        /// <param name="userId">用户名</param>
        /// <param name="paperId">试卷编号</param>
        /// <returns>-1未参加，0参加未提交，1参加并已提交</returns>
        public string GetStatusOfExamPaper(string userId, string paperId)
        {
            string status = "-1";
            string sql = "select status from Exam_Exams where userid='" + userId + "' and paperid='" + paperId + "'";
            object obj = DataAccess.getObject(sql);
            if (obj != null)
            {
                status = obj.ToString();
            }
            return status;
        }
        /// <summary>
        /// 插入一条考生答卷信息
        /// </summary>
        /// <param name="examId">答卷编号</param>
        /// <param name="userId">用户名</param>
        /// <param name="paperId">试卷编号</param>
        /// <param name="score">得分</param>
        /// <param name="comment">试卷评价</param>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="status">提交状态</param>
        /// <returns></returns>
        public bool InsertExamPaper(string examId, string userId, string paperId, int score, string comment, DateTime beginTime, DateTime endTime, string status)
        {
            bool flag = false;
            string sql = "insert into Exam_Exams values('" + examId + "','" + userId + "','" + paperId + "'," + score + ",'" + comment + "','" + beginTime + "','" + endTime + "','" + status + "')";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 修改考卷状态
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="paperId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool UpdateStatusOfExamPaper(string userId, string paperId, string status)
        {
            bool flag = false;
            string sql = "update Exam_Exams set status='" + status + "' where userid = '" + userId + "' and paperid='" + paperId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 保存试卷中一道题目的答案
        /// </summary>
        /// <param name="answerId"></param>
        /// <param name="examId"></param>
        /// <param name="questionId"></param>
        /// <param name="score"></param>
        /// <param name="answerContent"></param>
        /// <returns></returns>
        public bool SaveExamAnswers(string answerId, string examId, string questionId, int score, string answerContent)
        {
            bool flag = false;
            //有记录就删除
            string sqlDel = "delete from Exam_Answers where examid='" + examId + "' and questionId='" + questionId + "'";
            list.Add(sqlDel);
            //然后插入
            string sqlIns = "insert into Exam_Answers values('" + answerId + "','" + examId + "','" + questionId + "'," + score + ",'" + answerContent + "')";
            list.Add(sqlIns);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 获取已提交过的考卷Id
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="paperId"></param>
        /// <returns></returns>
        public string GetExamId(string userId, string paperId)
        {
            string examId = string.Empty;
            string sql = "select examid from Exam_Exams where userid='" + userId + "' and paperid='" + paperId + "'";
            object obj = DataAccess.getObject(sql);
            if (obj != null)
            {
                examId = obj.ToString();
            }
            return examId;
        }
        /// <summary>
        /// 绑定已保存的题目答案
        /// </summary>
        /// <param name="DataListBigQuestion">相当于试卷</param>
        /// <param name="examId">考卷编号</param>
        public void BindHadSaveAnswer(DataList DataListBigQuestion,string examId)
        {
            int i, j, k, l;
            string itemType = string.Empty;
            string questionId = string.Empty;
            string answerContent = string.Empty;
            string sql = "select a.questionid,itemtype,a.score,answercontent from Exam_Answers as a left join Exam_Questions as b on a.questionid=b.questionid where examid='" + examId + "'";
            DataTable dt = DataAccess.getDataTable(sql);
            foreach (DataRow row in dt.Rows)
            {
                //itemType = row["itemtype"].ToString();
                questionId = row["questionid"].ToString();
                answerContent = row["answercontent"].ToString();
                for (i = 0; i < DataListBigQuestion.Items.Count; i++)
                {
                    itemType = DataListBigQuestion.DataKeys[i].ToString();
                    DataList DataListQuestions = DataListBigQuestion.Items[i].FindControl("DataListQuestions") as DataList;
                    for (j = 0; j < DataListQuestions.Items.Count; j++)
                    {
                        if (questionId == DataListQuestions.DataKeys[j].ToString())
                        {
                            switch (itemType)
                            {
                                case "00001":
                                    RadioButtonList RabJudge = (RadioButtonList)DataListQuestions.Items[j].FindControl("RabJudge");
                                    if (answerContent != "")
                                    {
                                        RabJudge.SelectedValue = answerContent;
                                    }
                                    break;
                                case "00002":
                                    DataList DataListRadio = DataListQuestions.Items[j].FindControl("DataListRadio") as DataList;
                                    for (k = 0; k < DataListRadio.Items.Count; k++)
                                    {
                                        RadioButton RabItem = (RadioButton)DataListRadio.Items[k].FindControl("RabItem");
                                        Label LabRadioItem = (Label)DataListRadio.Items[k].FindControl("LabRadioItem");
                                        if (answerContent != "" && answerContent == LabRadioItem.Text)
                                        {
                                            RabItem.Checked = true;
                                        }
                                    }
                                    break;
                                case "00003":
                                    DataList DataListMultiple = DataListQuestions.Items[j].FindControl("DataListMultiple") as DataList;
                                    if (answerContent.IndexOf(',') == -1)//就选了一个答案
                                    {
                                        if (answerContent != "")
                                        {
                                            for (k = 0; k < DataListMultiple.Items.Count; k++)
                                            {
                                                CheckBox ChbItem = (CheckBox)DataListMultiple.Items[k].FindControl("ChbItem");
                                                Label LabMultipleItem = (Label)DataListMultiple.Items[k].FindControl("LabMultipleItem");

                                                if (answerContent == LabMultipleItem.Text)
                                                {
                                                    ChbItem.Checked = true;
                                                }
                                            }
                                        }
                                    }
                                    else//有逗号分隔，说明有多个答案
                                    {
                                        string[] arr = answerContent.Split(',');
                                        for (l = 0; l < arr.Length; l++)
                                        {
                                            for (k = 0; k < DataListMultiple.Items.Count; k++)
                                            {
                                                CheckBox ChbItem = (CheckBox)DataListMultiple.Items[k].FindControl("ChbItem");
                                                Label LabMultipleItem = (Label)DataListMultiple.Items[k].FindControl("LabMultipleItem");

                                                if (arr[l] == LabMultipleItem.Text)
                                                {
                                                    ChbItem.Checked = true;
                                                }
                                            }
                                        }
                                    }
                                    break;
                                case "00004":
                                    Label LabSubject = (Label)DataListQuestions.Items[j].FindControl("LabSubject");
                                    string[] arrBlank = LabSubject.Text.Trim().Split('（');
                                    string[] arrAnswer = answerContent.Split('|');
                                    int textNum = arrBlank.Length - 1;
                                    for (l = 1; l <= textNum; l++)
                                    {
                                        //即使动态生成时拼装id，依然找不到控件，看来是原则性错误，要想找到，需在Page_Load 之前生成？
                                        TextBox BlankTextBox = (TextBox)DataListQuestions.Items[j].FindControl("BlankTextBox" + l.ToString());
                                        BlankTextBox.Text = arrAnswer[l - 1];
                                    }
                                    break;
                                case "00005":
                                    TextBox TxtAnswer = (TextBox)DataListQuestions.Items[j].FindControl("TxtAnswer");
                                    TxtAnswer.Text = answerContent;
                                    break;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 获取可批阅试卷列表
        /// </summary>
        /// <param name="paperName">查询条件-试卷名称</param>
        /// <param name="userName">查询条件-用户名称</param>
        /// <returns></returns>
        public DataTable GetExamPaperForMark(string paperName,string userName)
        {
            DataTable dt = null;
            string sql = "select examid,papername,username,a.score,a.status from Exam_Exams as a left join Exam_Papers as b on a.paperid=b.paperid left join Users as c on a.userid=c.userid where a.status<>'0'";
            if (paperName != "")
            {
                sql += " and papername like '%" + paperName + "%'";
            }
            if (userName != "")
            {
                sql += " and username like '%" + userName + "%'";
            }
            dt = DataAccess.getDataTable(sql);
            return dt;
        }
        /// <summary>
        /// 根据考卷Id相关信息
        /// </summary>
        /// <param name="examId"></param>
        /// <returns></returns>
        public DataTable GetRecordOfExam(string examId)
        {
            DataTable dt = null;
            string sql = "select paperid,username,score,status,comment from Exam_Exams left join Users on Exam_Exams.userid=Users.userid  where examid='" + examId + "'";
            dt = DataAccess.getDataTable(sql);
            return dt;
        }
        /// <summary>
        /// 获取一条答案记录
        /// </summary>
        /// <param name="examId">考卷编号</param>
        /// <param name="questionId">题目编号</param>
        /// <returns></returns>
        public DataTable GetRecordOfAnswer(string examId, string questionId)
        {
            DataTable dt = null;
            string sql = "select score,answercontent from Exam_Answers where examid='" + examId + "' and questionid='" + questionId + "'";
            dt = DataAccess.getDataTable(sql);
            return dt;
        }
        /// <summary>
        /// 批阅试卷时修改主观题得分
        /// </summary>
        /// <param name="examId"></param>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public bool UpdateScoreOfObjectiveQuestions(string examId, string questionId, int score)
        {
            bool flag = false;
            string sql = "update Exam_Answers set score =" + score + " where examid='" + examId + "' and questionid='" + questionId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 修改考卷得分，考卷状态，评语
        /// </summary>
        /// <param name="examId"></param>
        /// <param name="score"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public bool SetWinScore(string examId, int score, string comment)
        {
            bool flag = false;
            string sql = "update Exam_Exams set score =" + score + ",comment='" + comment + "' where examid='" + examId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 结束批阅，修改考卷状态为已批阅
        /// </summary>
        /// <param name="examId"></param>
        /// <returns></returns>
        public bool EndMarkPaper(string examId)
        {
            bool flag = false;
            string sql = "update Exam_Exams set status ='2' where examid='" + examId + "'";
            list.Add(sql);
            if (DataAccess.execute(list))
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 获取我的已批阅的试卷
        /// </summary>
        /// <param name="paperName"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetMyExams(string paperName, string userId)
        {
            DataTable dt = null;
            string sql = "select examid,papername,a.score,begintime,endtime from Exam_Exams as a left join Exam_Papers as b on a.paperid=b.paperid where userid='"+userId+"' and a.status='2'";
            if (paperName != "")
            {
                sql += " and papername like '%" + paperName + "%'";
            }
            dt = DataAccess.getDataTable(sql);
            return dt;
        }
        #endregion
    }
}
